package com.hook3.pianke.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hook3.pianke.Adapter.SuiPianAdapter;
import com.hook3.pianke.Net.Url;
import com.hook3.pianke.R;
import com.hook3.pianke.activity.base.MyGridView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * 碎片
 * Created by HOME on 2015/12/8.
 */
public class SuiPianActivity {
    private static final String TAG =SuiPianActivity.class.getSimpleName() ;
    private Activity activity;
    private Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;
    private ListView listView;
    private List<ItemData> fragmentinfos;
    private  View titleVeiw;
    private ImageView iv_tag_suipian;
    private ImageView iv_suipian_write;
    private Intent intent;

    public SuiPianActivity(Activity activity, Toolbar toolbar, FrameLayout frameLayout) {
        this.activity = activity;
        this.toolbar = toolbar;
        this.frameLayout = frameLayout;
        init();
    }
    public class Json{
        public Data data;
        public int result;
    }
    public class Data{
        public List<ItemData> list;
        public int total;
    }
    public class ItemData{
        public long addtime;
        public String addtime_f;
        public String content;
        public String contentid;
        public CounterList counterList;
        public String coverimg;
        public String coverimg_wh;
        public boolean islike;
        public String songid;
        public TagInfo tag_info;
        public UserInfo userinfo;
    }
    public class CounterList{
        public  int comment;
        public  int like;

    }
    public class TagInfo{
        public int count;
        public boolean offical;
        public String tag;



    }
    public class UserInfo{
        public String icon;
        public int uid;
        public String uname;

    }

    /**
     * 实例化自己要实现的内容
     */
    public void init() {
        toolbar.setTitle("");
        titleVeiw = View.inflate(activity, R.layout.suipian_title,null);
        iv_tag_suipian = (ImageView) titleVeiw.findViewById(R.id.iv_tag_suipian);
        iv_suipian_write = (ImageView) titleVeiw.findViewById(R.id.iv_suipian_write);
        iv_tag_suipian.setOnClickListener(new MySuiPianOnClickListener());

        toolbar.addView(titleVeiw);
        toolbar.setTag(titleVeiw);

        listView = new ListView(activity);
        listView.setVerticalScrollBarEnabled(false);  //隐藏滚动条
        listView.setOnItemClickListener(new MyOnItemClickListener());

        processData();//联网获取数据

    }

    private void processData() {
        RequestParams params = new RequestParams(Url.SuiPian);
        params.addBodyParameter("client","2");
        params.addBodyParameter("start","0");
        params.addBodyParameter("limit","10");
        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                //解析json
                Gson gson = new Gson();

                fragmentinfos = gson.fromJson(result, Json.class).data.list;
                listView.setAdapter(new SuiPianAdapter(fragmentinfos, activity));
                frameLayout.setTag(listView);
                frameLayout.addView(listView);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {


            }

            @Override
            public void onFinished() {


            }


        });
    }

    private class MyOnItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        }
    }

    /**
     * 头部的点击监听
     */
    private class MySuiPianOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.iv_tag_suipian://进入标签页面
                    intent = new Intent(activity,TimeLinesTag.class);
                    activity.startActivity(intent);
                    break;

                case R.id.iv_suipian_write://进入发表页面
                    Toast.makeText(activity,"发表说说",Toast.LENGTH_SHORT).show();

                    break;
            }
        }
    }
}

